
/**
 * This is a class to handle market place
 * @author code traders
 * @version 1.0
 */
public class ItemMarketPlace {
	
	/**
	 * current ship
	 */
	private final Ship currentShip;
	
	/**
	 * current error value
	 */
	private int error = -1;

	/** for ONE **/
	private static final int ONE = 1;
	
	/** for TWO **/
	private static final int TWO = 2;
	
	/** for THREE **/
	private static final int THREE = 3;
	
	/** for FOUR **/
	private static final int FOUR = 4;
	
	/** for FIVE **/
	private static final int FIVE = 5;

	/** for SIX **/
	private static final int SIX = 6;
	
	/** for SEVEN **/
	private static final int SEVEN = 7;
	
	/** for EIGHT **/
	private static final int EIGHT = 8;
	
	/** for NINE **/
	private static final int NINE = 9;
	
	/** for TEN **/
	private static final int TEN = 10;
	
	/** for ELEVEN **/
	private static final int ELEVEN = 11;
	
	/** for TWELVE **/
	private static final int TWELVE = 12;

	/**
	 * for singleton
	 */
	public ItemMarketPlace() {
		this.currentShip = (Ship) Singleton.INSTANCE.get("ship");
	}
	
	/**
	 * to buy weapon
	 * @param w
	 * @return if weapon is added true, otherwise false. 
	 */
	public boolean canBuyWeapon(Weapon w) {
		if (!currentShip.canAddWeapon()) {
			error = ONE;
			return false;
		} else if (w.getPrice() > currentShip.getMoney()) {
			error = TWO;
			return false;
		}
		if(!currentShip.canAddWeapon(w)){
			return false;
		}
		currentShip.subtractMoney(w.getPrice());
		return true;
	}
	
	/**
	 * to buy shield
	 * @param s
	 * @return if shield is added true, otherwise false.
	 */
	public boolean canBuyShield(Shield s) {
		if (!currentShip.canAddShield()) {
			error = THREE;
			return false;
		} else if (s.getPrice() > currentShip.getMoney()) {
			error = FOUR;
			return false;
		}
		if(!currentShip.canAddShield(s)){
			return false;
		}
		currentShip.subtractMoney(s.getPrice());
		return true;
	}
	
	/**
	 * to buy gadget
	 * @param g
	 * @return if gadget is added true, otherwise false
	 */
	public boolean canBuyGadget(Gadget g) {
		if (!currentShip.canAddGadget()) {
			error = FIVE;
			return false;
		} else if (g.getPrice() > currentShip.getMoney()) {
			error = SIX;
			return false;
		}
		if(!currentShip.canAddGadget(g)){
			return false;
		}
		currentShip.subtractMoney(g.getPrice());
		return true;
	}
	
	/**
	 * to buy ship 
	 * @param s
	 * @return if ship is added true, otherwise false
	 */
	public boolean canBuyShip(Ships s) {
		if (!currentShip.canBuyShip(s)) {
			error = SEVEN;
			return false;
		} else if (s.getMinTechLevel().ordinal() >
		currentShip.getCurrentPlanet().getTechLevel().ordinal()) {
			error = EIGHT;
			return false;
		}
		currentShip.addMoney(currentShip.getShipType().getPrice());
		currentShip.changeShip(s);
		currentShip.subtractMoney(s.getPrice());
		return true;
	}
	
	/**
	 * to buy fuel
	 * @param n
	 * @return if fuel is added true, otherwise false
	 */
	public boolean canBuyFuel(int n) {
		if (currentShip.getMoney() < currentShip.getShipType().getFuelCost()) {
			error = NINE;
			return false;
		}
		currentShip.addFuel(n);
		currentShip.subtractMoney(currentShip.getShipType().getFuelCost());
		return true;
	}
	
	/**
	 * to sell weapon
	 * @param w
	 * @return if weapon is removed true, otherwise false
	 */
	public boolean canSellWeapon(Weapon w) {
		if (!currentShip.hasWeapon(w)) {
			error = TEN;
			return false;
		}
		currentShip.removeWeapon(w);
		return true;
	}
	
	/**
	 * to sell shield
	 * @param s
	 * @return if shield is removed true, otherwise false
	 */
	public boolean canSellShield(Shield s) {
		if (!currentShip.hasShield(s)) {
			error = ELEVEN;
			return false;
		}
		currentShip.removeShield(s);
		return true;
	}
	
	/**
	 * to sell gadget
	 * @param g
	 * @return if gadget is removed true, otherwise false
	 */
	public boolean canSellGadget(Gadget g) {
		if (!currentShip.hasGadget(g)) {
			error = TWELVE;
			return false;
		}
		currentShip.removeGadget(g);
		return true;
	}
	
	/**
	 * to send error message;
	 * @return error
	 */
	public String getErrorMsg() {
		switch (error) {
		case ONE:
			return "There is no more slot to add a weapon";
		case TWO:
			return "There is not enough money to buy the weapon";
		case THREE:
			return "There is no more slot to add a shield";
		case FOUR:
			return "There is not enough money to buy the shield";
		case FIVE:
			return "There is no more slot to add a gadget";
		case SIX:
			return "There is not enough money to buy the gadget";
		case -1:
			return "No Error";
		default:
			return getErrorMsgCont();
		}
	}
	
	/**
	 * to send proper message
	 * @return message
	 */
	private String getErrorMsgCont() {
		switch (error) {
		case SEVEN:
			return "There is not enough money to change the ship";
		case EIGHT:
			return "Higher tech-level is required to buy the ship";
		case NINE:
			return "There is not enough money to buy fuel";
		case TEN:
			return "There is no weapon available to remove";
		case ELEVEN:
			return "There is no shield available to remove";
		case TWELVE:
			return "There is no gadget available to remove";
		default:
			return "Invalid Error";
		}
	}
	
	/**
	 * This is a toString method.
	 * @return tostring.
	 */
	@Override
	public String toString() {
		return super.toString();
	}

}
